import { Component, OnInit, ViewChild } from '@angular/core';
import { NavigationExtras, Router } from '@angular/router';
import { select, Store } from '@ngrx/store';
import { ConfirmationService, MessageService } from 'primeng/api';
import { Table } from 'primeng/table';
import { Well } from 'src/app/api/appconfig';
import { AppMainComponent } from 'src/app/app.main.component';
import { UtilsService } from 'src/app/service/utils.service';
import { WellService } from 'src/app/service/well.service';
import { AppState } from 'src/app/store/app.model';
import { getApplicant } from 'src/app/store/utils/utils.reducer';

@Component({
    selector: 'app-hydrant-basicinfo',
    templateUrl: './hydrant-basicinfo.component.html',
    styleUrls: ['./hydrant-basicinfo.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class HydrantBasicinfoComponent implements OnInit {
    loading: boolean = true;
    //customers1: Customer[];
    display: boolean = false;
    editTag: boolean = false;
    @ViewChild('dt1') table: Table;
    data: Well[];
    address: any[];
    status= [
        { name: '正常运行', code: 1001 },
        { name: '尚未安装', code: 1002 },
        { name: '已经停用', code: 1003 },
        { name: '正在报警', code: 4444 },
    ];
    wellStatus: string;
    user: any;
    searchWellCode: any = '';
    searchWellName: any = '';
    searchAddress: any = '';
    searchApplicantName: any = '';
    searchWellId: any = '';
    applicant: any = [];
    well = {
        address: null,
        applicantId: null,
        applicantName: null,
        comNumber: null,
        createTime: new Date(),
        irrigationArea: null,
        latitude: null,
        longitude: null,
        phone: null,
        remark: null,
        status: null,
        wellCaliber: null,
        wellCode: null,
        wellDepth: null,
        wellId: null,
        wellName: null,
        wellType: null,
        wellUsage: null,
        zoneId: null,
        zoneType: null,
    };
    blankwell = {
        address: null,
        applicantId: null,
        applicantName: null,
        comNumber: null,
        createTime: new Date(),
        irrigationArea: null,
        latitude: null,
        longitude: null,
        phone: null,
        remark: null,
        status: null,
        wellCaliber: null,
        wellCode: null,
        wellDepth: null,
        wellId: null,
        wellName: null,
        wellType: null,
        wellUsage: null,
        zoneId: null,
        zoneType: null,
    };
    getStatus(item) {
        if (item === 1001) {
            return '正常运行';
        } else if (item === 1002) {
            return '尚未安装';
        } else if (item === 1003) {
            return '已经停用';
        } else {
            return '正在报警';
        }
    }
    genTwoLengthNumberString = (n) => (n >= 10 ? n : '0' + n);
    dateTimeTrans(d) {
        if (typeof d === 'string') {
            return d;
        }
        if (!d) {
            return '';
        }
        let yy = d.getFullYear();
        let MM = this.genTwoLengthNumberString(d.getMonth() + 1);
        let dd = this.genTwoLengthNumberString(d.getDate());
        let hh = this.genTwoLengthNumberString(d.getHours());
        let mm = this.genTwoLengthNumberString(d.getMinutes());
        let ss = this.genTwoLengthNumberString(d.getSeconds());
        return yy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss;
    }
    //messageService: any;
    constructor(
        private confirmationService: ConfirmationService,
        private messageService: MessageService,
        public appMain: AppMainComponent,
        private router: Router,
        private store: Store<AppState>,
        private wellService: WellService,
        private utilsService: UtilsService
    ) {}
    ngOnInit(){
        this.search();
        this.store.pipe(select(getApplicant)).subscribe((data)=>{
            this.applicant = data
            console.log(this.applicant)
        })
        this.address = this.utilsService.addressOption()
        console.log(this.address)
    }
    showDialog(a: boolean) {
        this.editTag = a;
        this.display = !this.display;
    }
    showDialog2(item) {
        this.editTag = true;
        this.display = !this.display;
        this.well = {
            ...item,
        };
    }
    deleteWell(item) {
        //console.log("ok")
        this.confirmationService.confirm({
            message: '请问你确认删除吗 ?',
            header: '警告',
            icon: 'pi pi-exclamation-triangle',
            accept: () => {
                this.messageService.add({
                    severity: 'info',
                    summary: '确认',
                    detail: '你已经删除',
                });
            },
        });
    }
    goToFile(a) {
        let navigationExtras: NavigationExtras = {
            queryParams: { id: a.wellId },
        };
        this.router.navigate(['/pages/hydrantfile'], navigationExtras);
    }
    async search() {
        //console.log('searchWellCode', this.searchWellCode);
        //console.log('searchWellName', this.searchWellName);
        //console.log('searchApplicantName', this.searchApplicantName);
        //console.log('searchAddress', this.searchAddress);
        const res = await this.wellService.fetchWellInfoByConfig({
            wellCode: this.searchWellCode || undefined,
            wellName: this.searchWellName || undefined,
            applicantName: this.searchApplicantName || undefined,
            address: this.searchAddress || undefined,
            wellId: this.searchWellId || undefined,
        });
        this.data = res.data;
        console.log(this.data);
    }
    async reset() {
        this.searchWellCode = '';
        this.searchWellName = '';
        this.searchApplicantName = '';
        this.searchAddress = '';
        this.search();
    }
    async handleStatus(row) {
        console.log(row);
        let nextStatus;
        if (row.status === 1002 || row.status === 1003) {
            nextStatus = 1001;
        } else if (row.status === 1001 || row.status === 4444) {
            nextStatus = 1003;
        } else {
            return;
        }
        const res = await this.wellService.updateWellInfoByConfig({
            ...row,
            status: nextStatus,
        });
        if (res.code == 200) {
        }
        this.search();
    }
    async handleDelete(row) {
        var temp = confirm('此操作将永久删除该数据, 是否继续?');
        if (temp) {
            let i = 0;
            const res = await this.wellService.deleteWellInfoById({
                well_ids: row.id,
            });
            if (res.code == 200) {
            }
            this.search();
        }
    }
}
